 //
//  AppDelegate.swift
//  YunCang
//
//  Created by wangshuning on 16/7/19.
//  Copyright © 2016年 wangshuning. All rights reserved.
//

 import UIKit
 
 import MMDrawerController
 
//import MMDrawerVisualState
#if DEBUG
let GY_APP_ID = "8e39b86cef4eb93d0380d99a24a573da" //自己的app-id
 #else
let GY_APP_ID = "daa41ad8c3b760e829b9a92477ba8b17" //自己的app-id

 #endif
 

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var drawerController: MMDrawerController?
    var loginCVC: LoginViewController?
    var result:Bool?
    var splashScreenView:GuideView?
    var isBackGround = false
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        //启动基本SDK
        Thread.sleep(forTimeInterval: 2.0)
        //启动更新检查SDK
        ConfigGlobal.installConfig()
        PgyManager.shared().start(withAppId: GY_APP_ID)
        if userInfo?.isAutomicUpdate == true{
            PgyUpdateManager.sharedPgy().start(withAppId: GY_APP_ID)
            PgyUpdateManager.sharedPgy().checkUpdate()
        }
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.backgroundColor =  UIColor(red: 0.150, green: 0.596, blue: 0.551, alpha: 1.000)
        PgyUpdateManager.sharedPgy().checkUpdate(withDelegete: self, selector: #selector(AppDelegate.getLastestVersion(_:)))
        //判断是否登录
        if UserDefaults.standard.bool(forKey: "Login"){
            //设置中间视图
            let  yunCVC = YunCangViewController()
            let  centerNVC = YunCenterViewController(rootViewController: yunCVC)
            //设置左边抽屉视图
            let leftVC = YunLeftTableViewController()
            //对抽屉效果进行设置
            drawerController = MMDrawerController.init(center: centerNVC, leftDrawerViewController: leftVC)
            drawerController?.showsShadow = false
            drawerController?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.all
            drawerController?.maximumLeftDrawerWidth = 270
            window?.rootViewController = drawerController
      
        }else{
            loginCVC = LoginViewController()
            window?.rootViewController = loginCVC
        }
        window?.makeKeyAndVisible()
        self.splashScreenView = GuideView.init(frame: self.window!.bounds, defaultImage: UIImage.init(named: "1242闪屏")!)
        
        self.window?.addSubview(self.splashScreenView!)
        appActiveTime = Date()
        UserDefaults.standard.set(false, forKey: "Login")
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        ConfigGlobal.saveConfig()
        CaptuvoDecoderManager.shareCaptuvoDecoderManager.captuvoDisconnected()
    }
    
    func getLastestVersion(_ sender: AnyObject?) {
        lastestVersionInfo = sender as? [String : String]
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        isBackGround = true
        self.timeaa(0)
        UIApplication.shared.beginBackgroundTask (expirationHandler: {
////            let timer = NSTimer.scheduledTimerWithTimeInterval(20, invocation: <#T##NSInvocation#>, repeats: false)
////            NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(AppDelegate.self.suntime), userInfo: nil, repeats: false)
        })
    }
    func timeaa(_ t: Int){
        print(t)
        if isBackGround == false{
            return
        }
        if t == 600 {
            exit(0)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(__int64_t(1 * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) {
            self.timeaa(t + 1)
        }
    }
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        isBackGround = false
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        CaptuvoDecoderManager.shareCaptuvoDecoderManager.captuvoConnected()
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

}

